package com.jcfy.project.module.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcfy.common.utils.bean.BeanUtils;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.framework.base.ClientBaseController;
import com.jcfy.framework.base.Error;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.framework.web.page.TableDataInfo;
import com.jcfy.project.module.domain.TGoods;
import com.jcfy.project.module.domain.TShopCar;
import com.jcfy.project.module.domain.TTeam;
import com.jcfy.project.module.domain.TUser;
import com.jcfy.project.module.service.ITGoodsService;
import com.jcfy.project.module.service.ITShopCarService;
import com.jcfy.project.module.service.ITTeamService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 商品Controller
 *
 * @author hushuai
 * @date 2020-08-16
 */
@RestController
@RequestMapping("/client/shopcar")
public class ShopCarController extends ClientBaseController {
    @Autowired
    private ITGoodsService tGoodsService;
    @Autowired
    private ITTeamService teamService;
    @Autowired
    private ITShopCarService shopCarService;


    /**
     * 购物车列表
     */
    @PostMapping("/list")
    public AjaxResult list(TShopCar shopCar, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
        List<TShopCar> list = shopCarService.selectTShopCarList(shopCar);
//        PageInfo<Object> pageInfo = new PageInfo(list);
        Map<Long,Object> goodsCart = new HashMap<>();

        list.forEach(item->{
            Map<String,Object> data = new HashMap<>();
            TGoods goods = tGoodsService.selectTGoodsById(item.getGoodsId());
            data.put("goods", goods);
            data.put("count", item.getNumber());
            goodsCart.put(goods.getGoodsId(),data);
        });
//        pageInfo.setList(goodsCart);
        return AjaxResult.success(goodsCart);
    }

    /**
     * 添加到购物车
     */
    @PostMapping("/add")
    public AjaxResult add(TShopCar shopCar) {
        Long userId = this.getSessionUserId();
        userId = 2l;

        TGoods goods = tGoodsService.selectTGoodsById(shopCar.getGoodsId());

        shopCar.setUserId(userId);
        shopCar.setSupplierId(goods.getSupplierId());
        shopCar.setGoodsNo(goods.getGoodsNo());
        shopCar.setGoodsName(goods.getName());
        shopCar.setPrice(goods.getFavorablePrice());
        shopCar.setSpecifications(goods.getSpecifications());
        shopCar.setChecked(0);
        shopCar.setPicUrl(goods.getPicUrl());

        // 查询是否已存在相同商品
        TShopCar params = new TShopCar();
        params.setGoodsId(shopCar.getGoodsId());
        params.setUserId(userId);
        List<TShopCar> old = shopCarService.selectTShopCarList(params);
        int ko;
        if (CollectionUtils.isEmpty(old)){
            ko = shopCarService.insertTShopCar(shopCar);
        }else if (shopCar.getNumber() == 0) {
            ko = shopCarService.deleteTShopCarById(old.get(0).getCarId());
        }else {
            shopCar.setCarId(old.get(0).getCarId());
            ko = shopCarService.updateTShopCar(shopCar);
        }

        if (ko>0) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error(Error._200301);
        }
    }



}
